package com.ddlaw.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ddlaw.entity.TContractType;
import com.ddlaw.entity.TLawContracts;
import com.ddlaw.enums.UploadEnum;
import com.ddlaw.service.ContractsService;
import com.ddlaw.service.KnowledgeService;
import com.ddlaw.service.LawyerService;
import com.ddlaw.service.UserService;
import com.ddlaw.utils.Doc2Html;
import com.ddlaw.utils.Upload;

/**
 * 
 *@author YunLong
 *@time 下午8:45:25
 */
@Controller
@RequestMapping(value = "/product")
public class ProductController {
	
	@Resource
	UserService userService;
	@Resource
	LawyerService lawyerService;
	@Resource
	ContractsService contractsService;
	@Resource
	KnowledgeService knowledgeService;
	
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String productListV(Model model) {
		List<TLawContracts> contracts = contractsService.getAllContracts();
		model.addAttribute("contracts", contracts);
		return "product/index";
	}
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addProductV(Model model) {
		return "product/add";
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String addProduct(TLawContracts contracts,HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		MultipartFile multipartFile = multipartRequest.getFile("file");
		String fileUrl = Upload.uploadFile(multipartFile, request,"docs/","");
		contracts.setFileUrl(fileUrl.replaceFirst("docs", "file"));
		contracts.setHtmlUrl(fileUrl.substring(0, fileUrl.lastIndexOf(".")) + ".html");
		String filePath = UploadEnum.FILEPATH.getValue() + fileUrl.substring(5);
		Doc2Html.convert(new File(filePath), UploadEnum.WORDHTML.getValue());
		int rs = contractsService.addContracts(contracts);
		if (rs > 0) {
			return "redirect:/product/index";
		}
		return "product/add";
	}
	
	@RequestMapping(value = "remove/{productId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> remove(@PathVariable(value = "productId") int productId) {
		Map<String, Object> result = new HashMap<String, Object>();
		int rs = contractsService.delContractsById(productId);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "删除成功！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "删除失败!请稍后再试！");
		return result;
	}
	
	@RequestMapping(value = "search", method = RequestMethod.POST)
	public String search(int contractId,Model model) {
		if (contractId == 0) {
			List<TLawContracts> contracts = contractsService.getAllContracts();
			model.addAttribute("contracts", contracts);
			model.addAttribute("contractId", contractId);
			return "product/index";
		}
		List<TLawContracts> contracts = contractsService.getContractsById(contractId);
		model.addAttribute("contracts", contracts);
		model.addAttribute("contractId", contractId);
		return "product/index";
	}
	
	/**
	 * 获取合同大类型对应小类型
	 * @param typeId 大类型ID
	 * @return		对应城市
	 */
	@RequestMapping(value="type",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> province( int typeId){
		Map<String,Object> result = new HashMap<String, Object>();
		List<TContractType> types = contractsService.getByTypeId(typeId);
		result.put("types", types);
		return result;
	}
}
